﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SaleOnlineSystem
{
    public partial class SummaryOrder : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            lblDate.Text = DateTime.Now.ToString();
            lblUser.Text = BOL.Shopping.ShoppingCart.Instance.customer.fullname;
            lblEmail.Text = BOL.Shopping.ShoppingCart.Instance.customer.email;
            lblPhone.Text = BOL.Shopping.ShoppingCart.Instance.customer.phonenumber;
            lblAddress.Text = BOL.Shopping.ShoppingCart.Instance.customer.address;
        }

        protected void btnMuahang_Click(object sender, EventArgs e)
        {
            string fullname = BOL.Shopping.ShoppingCart.Instance.customer.fullname;
            string address = BOL.Shopping.ShoppingCart.Instance.customer.address;
            string email = BOL.Shopping.ShoppingCart.Instance.customer.email;
            string phone = BOL.Shopping.ShoppingCart.Instance.customer.phonenumber;
            double totalprice = BOL.Shopping.ShoppingCart.Instance.getSum();
            string value1 = "N'" + fullname + "','" + email + "','" + phone + "',N'" + address + "',N'" + BOL.Shopping.ShoppingCart.Instance.payment + "','" + DateTime.Now.ToString() + "'," + totalprice + ",N'In process'";
            List<string> values = new List<string>();
            values.Add(value1);
            string idorder = DAL.DCollector.DBCollector.Instance.StoredProcedureInsertByRows("tbl_Orders", "insertorder", values, true).Rows[0]["nextid"].ToString();

            
            List<string> myvalue2 = new List<string>();
            List<string> myvalue3 = new List<string>();
            
            for (int i = 0; i < BOL.Shopping.ShoppingCart.Instance.mycart.Count; i++)
            {
                int nextid = Convert.ToInt32(DAL.DCollector.DBCollector.Instance.getNextID("tbl_OrderLine", "OrderLine_ID"));
                if (nextid == 0)
                {
                    string value2 = "1,'" + idorder + "'," + BOL.Shopping.ShoppingCart.Instance.mycart[i].getIDProduct() + "," + BOL.Shopping.ShoppingCart.Instance.mycart[i].getIDColor() + "," + BOL.Shopping.ShoppingCart.Instance.mycart[i].getIDSize() + "," + BOL.Shopping.ShoppingCart.Instance.mycart[i].soluong;
                    myvalue2.Add(value2);
                    DAL.DCollector.DBCollector.Instance.StoredProcedureInsertByRows("tbl_OrderLine", "insertbyrows", "*", myvalue2, false);
                }
                else
                {
                    int idorderline = nextid + 1;
                    string value3 = "" + idorderline + ",'" + idorder + "'," + BOL.Shopping.ShoppingCart.Instance.mycart[i].getIDProduct() + "," + BOL.Shopping.ShoppingCart.Instance.mycart[i].getIDColor() + "," + BOL.Shopping.ShoppingCart.Instance.mycart[i].getIDSize() + "," + BOL.Shopping.ShoppingCart.Instance.mycart[i].soluong;
                    myvalue3.Add(value3);
                    DAL.DCollector.DBCollector.Instance.StoredProcedureInsertByRows("tbl_OrderLine", "insertbyrows", "*", myvalue3, false);
                }               
            }
            
            HttpContext.Current.Session["SHOPPING"] = null;
            BOL.Shopping.ShoppingCart.Instance.dataItems = null;

            Response.Redirect("/trangchu.htm");
        }
    }
}